package com.filetransfer.view.Panel;
import com.filetransfer.api.ShareCollection;
import com.filetransfer.config.UserThemeConfiguration;
import com.filetransfer.controller.FileTransferController;
import com.filetransfer.view.Components.TaskPanel;
import com.filetransfer.viewmodel.DownloadTableViewModel;
import com.filetransfer.viewmodel.DownloadTableRowViewModel;
import de.exlll.configlib.YamlConfigurations;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import raven.toast.Notifications;

import javax.swing.*;
import java.io.IOException;

import static com.filetransfer.Main.CONFIG_FILE_PATH;
@Getter
@Slf4j
public class DownloadPanel extends TaskPanel<DownloadTableViewModel, DownloadTableRowViewModel> {
    private final FileTransferController controller;
    UserThemeConfiguration config = YamlConfigurations.update(CONFIG_FILE_PATH, UserThemeConfiguration.class);
    public DownloadPanel(FileTransferController controller) {
        super("暂时无文件下载", "icons/cloud_download.svg", new DownloadTableViewModel());
        this.controller = controller;
    }
    @Override
    protected void onActionButtonClick() {
        var window = SwingUtilities.getWindowAncestor(this);
        if (window instanceof JFrame frame) {
            SwingWorker<Void, Integer> worker = new SwingWorker<>() {
                @Override
                protected Void doInBackground() {
                    controller.inputShareCode(code -> {
//                                final DownloadTableRowViewModel row = new DownloadTableRowViewModel(taskName, "初始化中", 0, "");
                        controller.fetchShare(code, result -> {
                            //获取成功
                            log.info("获取成功");
                            controller.previewAndChooseLinksToDownload(result, files -> {
                                //获取成功
                                log.info("获取成功");
                                for (var file : files) {
                                    var fileName = file.getFileName();
                                    var link = ShareCollection.getDownloadLink(code, file);
                                    var row = new DownloadTableRowViewModel(fileName, "等待中", 0, link.toString());
                                    try {
                                        controller.prepareAndDownloadFile(link, config.getDownloadDirectory(), () -> {
                                                    addTask(row);
                                                    log.info("下载中");
                                                    row.setStatus("下载中");
                                                    getMessageLabel().setText("下载中...");
                                                }, (resultFile) -> {
                                                    log.info("下载完成" + resultFile.getAbsolutePath());
                                                    row.setStatus("完成");
                                                    row.setLocal(file.getFilePath());
                                                    getMessageLabel().setText("下载完成！");
                                                    Notifications.getInstance().show(
                                                            Notifications.Type.SUCCESS,
                                                            Notifications.Location.BOTTOM_LEFT,
                                                            "下载完成：" + resultFile.getAbsolutePath()
                                                    );
                                                },
                                                (err) -> {
                                                    log.info("下载失败: "+err.getMessage());
                                                    row.setStatus("下载失败: "+err.getMessage());
                                                    row.setProgress(0);
                                                    getMessageLabel().setText("暂无任务");
                                                    Notifications.getInstance().show(
                                                            Notifications.Type.ERROR,
                                                            Notifications.Location.BOTTOM_LEFT,
                                                            "下载失败"
                                                    );
                                                },
                                                row::setProgress);
                                    } catch (IOException e) {
                                        row.setStatus("下载失败：" + e.getMessage());
                                        row.setProgress(0);
                                        getMessageLabel().setText("暂无任务");
                                        log.error("下载失败", e);
                                    }
                                }
                            });
                        }, () -> {//获取失败
                            getMessageLabel().setText("暂无任务 | 获取失败");
                        });
                    });
//                    controller.openReceiveWindow(config.getDownloadDirectory(),
//                            () -> {
//                                addTask(row);
//                                log.info("下载中");
//                                row.setStatus("下载中");
//                                getMessageLabel().setText("下载中...");
//                            },
//                            () -> {
//                                log.info("下载完成");
//                                row.setStatus("完成");
//                                getMessageLabel().setText("下载完成！");
//                            },
//                            () -> {
//                                log.info("下载失败");
//                                row.setStatus("下载失败");
//                                row.setProgress(0);
//                                getMessageLabel().setText("暂无任务");
//                            },
//                            progress -> {
//                                row.setProgress(progress);
//                            },
//                            (link) -> {
//                                row.setLink(link.toString());
//                            });
                    return null;
                }
            };
            worker.execute();
        } else {
            System.err.println("无法获取 JFrame 实例，当前窗口类型为: " + window.getClass().getName());
        }
    }
}